package com.huawei.cloud.phone.management.common.exception;

import com.fasterxml.jackson.annotation.JsonIncludeProperties;
import com.huawei.cloud.phone.management.common.BeanFactory;
import com.huawei.cloud.phone.management.common.utils.I18nUtils;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

import java.util.Arrays;

@JsonIncludeProperties({"errorCode", "errorMsg"})
@AllArgsConstructor
@Getter
public abstract class BusinessException extends RuntimeException {
    static final long serialVersionUID = -7034897111145766939L;

    @Setter(value = AccessLevel.PROTECTED)
    private int httpCode;

    private String errorCode;

    private Object[] errorParams;

    @Setter(value = AccessLevel.PROTECTED)
    private String errorMsg;

    public BusinessException(String errorCode, Object... errorParams) {
        this.errorCode = errorCode;
        if (errorParams != null && errorParams.length != 0) {
            this.errorParams = Arrays.copyOf(errorParams, errorParams.length);
        }
        this.errorMsg = I18nUtils.getMessage(errorCode, errorParams);
    }
}
